package com.smi;

import com.smi.service.SqlGenerate;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;

import java.io.IOException;

public class Controller {

    @FXML
    private TextArea buttonStatusText;

    @FXML private TextField dbName;
    @FXML private TextField tableName;
    @FXML private ComboBox optionType;
    @FXML private TextArea logSql;


    @FXML
    protected void handleSubmitButtonAction(ActionEvent event) {


        String dbNameStr = dbName.getText();
        String tableNameStr = tableName.getText();
        String optionTypeStr = (String) optionType.getValue();
        String logSqlStr = logSql.getText();

        SqlGenerate generate = new SqlGenerate();
        String result = null;

        if(OptionType.INSERT.toString().equals(optionTypeStr) && (logSqlStr == null || "".equals(logSqlStr))) {
            result = generate.genInsert(dbNameStr, tableNameStr, false, false);
        } else if(OptionType.INSERT_UUID_AUTO.toString().equals(optionTypeStr) && (logSqlStr == null || "".equals(logSqlStr))) {
            result = generate.genInsert(dbNameStr, tableNameStr, true, true);
        } else if(OptionType.INSERT_UUID.toString().equals(optionTypeStr) && (logSqlStr == null || "".equals(logSqlStr))) {
            result = generate.genInsert(dbNameStr, tableNameStr, true, false);
        } else if(OptionType.UPDATE.toString().equals(optionTypeStr)) {
            result = generate.genUpdate(dbNameStr, tableNameStr);
        } else if(OptionType.INSERT.toString().equals(optionTypeStr) && logSqlStr != null && !"".equals(logSqlStr)) {
            result = generate.genInsertWithinLog(dbNameStr, tableNameStr, false, logSqlStr);
        } else if(OptionType.INSERT_UUID.toString().equals(optionTypeStr) && logSqlStr != null && !"".equals(logSqlStr)) {
            result = generate.genInsertWithinLog(dbNameStr, tableNameStr, true, logSqlStr);
        } else {
            result = "请输入SQL类型";
        }

        buttonStatusText.setText(result);
    }

    @FXML
    protected void handleSettingButtonAction(ActionEvent event) throws IOException {
        Stage stage=(Stage) getWindow(event);
        startSub(stage);
    }

    /**
     * 启动子窗口
     * @throws IOException
     */
    public void startSub(Window owner) throws IOException {
        Stage stage=new Stage();
        Parent settingPage = FXMLLoader.load(getClass().getClassLoader().getResource("setting.fxml"));
        Scene scene = new Scene(settingPage, 300, 250);

        stage.setScene(scene);
        stage.initModality(Modality.WINDOW_MODAL);
        //设置父窗口
        stage.initOwner(owner);

        stage.show();
    }

    /**
     * 获取控件元素的窗口对象
     * @param event
     * @return
     */
    public Window getWindow(ActionEvent event){
        return((Node)event.getSource()).getScene().getWindow();
    }
}
